package kuzmenkov.oip.controller.applet;

import java.awt.FlowLayout;
import java.text.Format;

import javax.swing.JFormattedTextField;
import javax.swing.JLabel;

/**
 * The class is a field ui component. It can be used for to input integer, float
 * or string value.
 * 
 * @author Michael Kuzmenkov
 * 
 */
public class FieldUIC extends ParamUIC {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2666493826633442560L;
	protected JFormattedTextField field;
	protected int columns;
	/**
	 * This argument defines a allowed type of  a input value
	 */
	protected Format format;
	//private static Logger log = Logger.getLogger(FieldUIC.class.getName());

	public FieldUIC(Param parameter, Format format, int numberOfColumns) {
		super(parameter);
		I18nTool itool = I18nTool.getInstance();
		if (numberOfColumns <= 0)
			throw new IllegalArgumentException(itool.getMessage(
					"FieldUIC.illegalNumberOfColumn",
					new Object[] { new Integer(numberOfColumns) }));
		this.columns = numberOfColumns;
		this.format = format;
		init();
	}

	public Object getValue() {
		field.updateUI();
		return field.getValue();
	}

	private void init() {
		Param param = getParam();
		String name = param.getName();
		String desc = param.getDescription();
		boolean isResult = param.isResult();

		if (format != null)
			field = new JFormattedTextField(format);
		else
			field = new JFormattedTextField();

		field.setColumns(columns);
		field.setToolTipText(truncateString(desc,MAX_TIP_LENGTH));
		field.setEditable(!isResult);
		

		JLabel label = new JLabel(name);
		setToolTipText(truncateString(desc,MAX_TIP_LENGTH));

		setLayout(new FlowLayout(FlowLayout.LEFT));

		add(label);
		add(field);

		// setBorder(BorderFactory.createLineBorder(Color.BLACK));
	}

	/**
	 * Sets the value into the component.
	 */
	public void setValue(Object value) {
		field.setValue(value);
	}

}
